import Vue from 'vue'
export default {
	namespaced:true,
	state:()=>({
		show:false, //控制规格弹窗显示隐藏
		overlayData:null,  //存放弹窗商品数据
		edit:-1,   //标志新增、编辑
		cartList:[
			{
				id:'11547',
				img:'https://static.mcake.com/new_goods/baiselianren/C0702/list/1.jpg',
				name:'白色恋人',
				french:'Blanc Amant',
				price:218,
				list:[
					{id: 11547, sku: "C0701", ahead: "提前5小时预定", edible: "2-3人食", spec: "1磅", price: "218.00",fittings:'3套餐具',weight:'200g'},
					{id: 11548, sku: "C0702", ahead: "提前5小时预定", edible: "4-7人食", spec: "2磅", price: "318.00",fittings:'5套餐具',weight:'500g'},
					{id: 11549, sku: "C0703", ahead: "提前5小时预定", edible: "8-12人食", spec: "3磅", price: "458.00",fittings:'10套餐具',weight:'800g'}
				],
				ischeck:true,
				num:2,
				dropIdx:0 //记录规格下标
			},
			{
				id:'11548',
				img:'https://static.mcake.com/new_goods/baiselianren/C0702/list/1.jpg',
				name:'白色恋人1',
				french:'Blanc Amant',
				price:219,
				list:[
					{id: 11547, sku: "C0701", ahead: "提前5小时预定", edible: "2-3人食", spec: "1磅", price: "218.00",fittings:'3套餐具',weight:'200g'},
					{id: 11548, sku: "C0702", ahead: "提前5小时预定", edible: "4-7人食", spec: "2磅", price: "318.00",fittings:'5套餐具',weight:'500g'},
					{id: 11549, sku: "C0703", ahead: "提前5小时预定", edible: "8-12人食", spec: "3磅", price: "458.00",fittings:'10套餐具',weight:'800g'}
				],
				ischeck:false,
				num:1,
				dropIdx:0 //记录规格下标
			}
		]
	}),
	getters:{
		allInfo(state){
			let allCheck = true
			let allPrice = 0 
			let allNum = 0
			state.cartList.forEach(item=>{
				if(!item.ischeck){
					allCheck = false
				}
				if(item.ischeck){
					allPrice += item.num * item.price
				}
				allNum += item.num
			})
			return {allCheck,allPrice,allNum}
		}
	},
	mutations:{
		cartAddMut(state,obj){ //新增
			console.log('编辑状态',obj);
			let {cartList,edit} = state
			if(edit!=-1){ // 【情况1】编辑购物车中已有数据
				cartList[edit] = obj
				state.cartList = [...cartList]  //处理勾选的响应式问题
				return
			}
			let len = cartList.length
			for(let i=0;i<len;i++){
				if(cartList[i].id==obj.id){  //【情况2】向购物车新增了已有商品
					state.cartList[i].num+=obj.num
					return
				}
			}
			Vue.set(obj,'ischeck',true)
			// Vue.set(obj,'num',1)
			state.cartList.push(obj) //【情况3】向购物车新增了新商品
		},
		cartCheckMut(state,idx){ //单选
			console.log(idx);
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
			console.log(state.cartList[idx].ischeck);
		},
		cartAllCheckMut(state,bool){ //全选控制单选
			state.cartList.forEach(item=>{
				item.ischeck = !bool
			})
		},
		cartOverlayMut(state,bool){ //弹窗遮罩控制
			state.show = bool
		},
		initOverlayDataMut(state,{obj,edit}){ //购物车按钮、编辑按钮，初始化弹窗数据
			state.overlayData = obj  //设置弹窗数据
			state.edit = edit
			state.show = true
		}
	}
}